<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="短信类型" prop="name">
        <el-input v-model="dataForm.name" :readonly="true" placeholder="短信类型"></el-input>
      </el-form-item>
      <el-form-item label="变量列表">
        <el-table :data="tableData" height="180" border style="width: 100%;">
          <el-table-column prop="variableName" label="变量名" width="180">
          </el-table-column>
          <el-table-column prop="describe" label="描述">
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="短信内容" prop="contentTemp">
        <el-input type="textarea" :autosize="{ minRows: 4}" placeholder="请输入短信内容" v-model="dataForm.contentTemp">
        </el-input>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-radio-group v-model="dataForm.state">
          <el-switch v-model="dataForm.state" :active-value="1" :inactive-value="0" active-text="启用" class="switch" inactive-text="禁用"></el-switch>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        list: {
          list1: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【积分数量】',
              describe: '显示多少积分'
            },
            {
              variableName: '【商品名称】',
              describe: '显示兑换的商品名称'
            },
          ],
          list2: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【入住次数】',
              describe: '总计入住次数'
            },
          ],
          list3: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【预定数量】',
              describe: '客人预定了多少间'
            },
            {
              variableName: '【订单号】',
              describe: '客人预定单号'
            },
            {
              variableName: '【入住日期】',
              describe: '客人入住日期'
            },
            {
              variableName: '【离店日期】',
              describe: '客人离店日期'
            },
          ],
          list4: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【入住房间数量】',
              describe: '客人登记入住了几间房'
            },
            {
              variableName: '【消费金额】',
              describe: '客人消费了多少金额'
            },
          ],
          list5: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【会员卡号】',
              describe: '客人会员卡号'
            },
            {
              variableName: '【消费金额】',
              describe: '会员卡消费了多少金额'
            },
            {
              variableName: '【余额】',
              describe: '会员卡剩余金额'
            },
          ],
          list6: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【充值金额】',
              describe: '客人会员卡充值了多少金额'
            },
            {
              variableName: '【赠送金额】',
              describe: '客人会员卡充值赠送了多少金额'
            },
            {
              variableName: '【赠送积分】',
              describe: '客人会员卡充值赠送了多少积分'
            },
            {
              variableName: '【余额】',
              describe: '客人会员卡剩余金额'
            },
            {
              variableName: '【积分余额】',
              describe: '客人会员卡剩余积分'
            },
          ],
          list7: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【扣除积分】',
              describe: '客人扣除了多少积分'
            },
            {
              variableName: '【剩余积分】',
              describe: '客人会员卡剩余积分'
            },
          ],
          list8: [{
              variableName: '【客人名称】',
              describe: '显示客人的名字'
            },
            {
              variableName: '【酒店名称】',
              describe: '显示当前酒店的名称'
            },
            {
              variableName: '【赠送积分】',
              describe: '给客人赠送了多少积分'
            },
            {
              variableName: '【剩余积分】',
              describe: '客人会员卡剩余积分'
            },
          ],
        },
        tableData: [],
        visible: false,
        dataForm: {
          id: 0,
          sellerId: '',
          name: '',
          contentTemp: '',
          creatTime: '',
          state: 1,
          type: '',
          isSys: '',
          isDel: ''
        },
        dataRule: {
          name: [{
            required: true,
            message: '短信类型不能为空',
            trigger: 'blur'
          }],
          contentTemp: [{
            required: true,
            message: '短信内容不能为空',
            trigger: 'blur'
          }],
          state: [{
            required: true,
            message: '状态不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/seller/tsellersmstemp/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tSellerSmsTemp.sellerId
                this.dataForm.name = data.tSellerSmsTemp.name
                this.dataForm.contentTemp = data.tSellerSmsTemp.contentTemp
                this.dataForm.creatTime = data.tSellerSmsTemp.creatTime
                this.dataForm.state = data.tSellerSmsTemp.state
                this.dataForm.type = data.tSellerSmsTemp.type
                this.dataForm.isSys = data.tSellerSmsTemp.isSys
                this.dataForm.isDel = data.tSellerSmsTemp.isDel
                this.tableData = this.list['list' + data.tSellerSmsTemp.type]
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seller/tsellersmstemp/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'name': this.dataForm.name,
                'contentTemp': this.dataForm.contentTemp,
                'creatTime': this.dataForm.creatTime,
                'state': this.dataForm.state,
                'type': this.dataForm.type,
                'isSys': this.dataForm.isSys,
                'isDel': this.dataForm.isDel
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
